// pages/init/init.js
//index.js
//获取应用实例
const app = getApp();
const that = this;
Page({
  data: {

  },
  onLoad: function () {
    var that = this;

    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        if (res.code) {
          // get code
          //console.log(res.code);
          var userCode = res.code;

          wx.request({
            url: app.globalData.server + '/token/',
            method: 'POST',
            data: {
              code: userCode
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded', // 默认值
              'x-wxapp-signature': null,
              'x-wxapp-timestamp': app.getTimestamp(),
              'x-wxapp-nonce': app.getRandom()
            },
            success: function (res) {
              // 设置token
              app.globalData.token = res.data.data.token;
              console.log("token:");
              console.log(res);

              // 测试用Token
              //专家用户
              //app.globalData.token = '225d1f245a302a7dde4485ae9cef349e';
              // 企业用户
              //app.globalData.token = '4c9c555512634970a7699c98d7c4f24f';
              // 游客
              //app.globalData.token = 'a1a835ee3d0ef290232ef392bbdfd334'



              // 获取userType
              wx.request({
                url: app.globalData.server + '/token/',
                method: 'GET',
                data: {},
                header: {
                  'content-type': 'application/x-www-form-urlencoded', // 默认值
                  'Authorization': 'Bearer ' + app.globalData.token,
                  'x-wxapp-signature': null,
                  'x-wxapp-timestamp': app.getTimestamp(),
                  'x-wxapp-nonce': app.getRandom()
                },
                success: function (res) {
                  console.log("用户类型：");
                  console.log(res);
                  app.globalData.userType = res.data.user_type;

                  //根据用户种类跳转
                  // 游客 
                  if (app.globalData.userType == 1) {
                    // 获得认证状态
                    wx.request({
                      url: app.globalData.server + '/user/company/auth/',
                      method: 'GET',
                      data: {},
                      header: {
                        'content-type': 'application/x-www-form-urlencoded', // 默认值
                        'Authorization': 'Bearer ' + app.globalData.token,
                        'x-wxapp-signature': null,
                        'x-wxapp-timestamp': app.getTimestamp(),
                        'x-wxapp-nonce': app.getRandom()
                      },
                      success: function (res) {
                        // 注册了企业用户还在等待审核
                        if (res.data.message != "no material") {
                          console.log("等待审核的游客");

                          app.globalData.companyStatus = res.data.data.status;
                          app.globalData.reason = res.data.data.reason;
                          console.log(app.globalData.companyStatus);

                          wx.redirectTo({
                            url: '../companyIndex/companyIndex',
                          })
                          // 还未注册的游客
                        } else {
                          console.log("未注册的游客");
                          app.globalData.userInfo = { "expert_avatar": "/media/avatars/default_avatar.png" };
                          console.log(app.globalData.userInfo);
                          app.globalData.userInfo.expert_avatar = app.globalData.server + app.globalData.userInfo.expert_avatar;
                          
                          wx.redirectTo({
                            url: '../index/index',
                          })
                        }
                      }
                    })
                    // 专家用户
                  } else if (app.globalData.userType == 2) {
                    // 获取userInfo
                    wx.request({
                      url: app.globalData.server + '/user/',
                      method: 'GET',
                      data: {},
                      header: {
                        'content-type': 'application/x-www-form-urlencoded', // 默认值
                        'Authorization': 'Bearer ' + app.globalData.token,
                        'x-wxapp-signature': null,
                        'x-wxapp-timestamp': app.getTimestamp(),
                        'x-wxapp-nonce': app.getRandom()
                      },
                      success: function (res) {
                        app.globalData.userInfo = res.data.data;
                        app.globalData.userInfo.expert_avatar = app.globalData.server + res.data.data.expert_avatar;
                        wx.redirectTo({
                          url: '../index/index',
                          //url:'../companyDetails/companyDetails?company_id=141'
                        })
                      }
                    })
                    // 企业用户
                  } else if (app.globalData.userType == 3) {
                    // 获取userInfo
                    wx.request({
                      url: app.globalData.server + '/user/',
                      method: 'GET',
                      data: {},
                      header: {
                        'content-type': 'application/x-www-form-urlencoded', // 默认值
                        'Authorization': 'Bearer ' + app.globalData.token,
                        'x-wxapp-signature': null,
                        'x-wxapp-timestamp': app.getTimestamp(),
                        'x-wxapp-nonce': app.getRandom()
                      },
                      success: function (res) {
                        app.globalData.userInfo = res.data.data;
                        app.globalData.userInfo.company_logo = app.globalData.server + res.data.data.company_logo;
                        //获取系统当前时间
                        var myDate = new Date();
                        var thisYear = myDate.getFullYear();
                        //console.log(thisYear);
                        app.globalData.thisYear = thisYear;


                        wx.request({
                          url: app.globalData.server + '/user/company/auth/',
                          method: 'GET',
                          data: {},
                          header: {
                            'content-type': 'application/x-www-form-urlencoded', // 默认值
                            'Authorization': 'Bearer ' + app.globalData.token,
                            'x-wxapp-signature': null,
                            'x-wxapp-timestamp': app.getTimestamp(),
                            'x-wxapp-nonce': app.getRandom()
                          },
                          success: function (res) {
                            
                            if (res.data.message != "no material") {

                              app.globalData.companyStatus = res.data.data.status;
                              app.globalData.reason = res.data.data.reason;
                            } else {
                              app.globalData.companyStatus = "已通过";
                              app.globalData.reason = "已通过";
                            }

                            wx.redirectTo({
                              url: '../companyIndex/companyIndex',
                            })
                          }
                        })
                      }
                    })
                  }
                }
              });
            }
          })
        }
      }
    })
  }
})
